package jason;
import interfaces.InterfaceServidor;
import jason.asSyntax.Literal;
import jason.asSyntax.Structure;
import jason.environment.Environment;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import robocode.rescue.ConstantesExecucao;
import robocode.robotinterfaces.IBasicRobot;

import net.sf.robocode.core.Container;

import conexao.Cliente;

public class Ambiente extends Environment {

	private final int INDEX_TEAM = 0;
	private final int INDEX_AGENTE = 1;

	private Cliente cliente;
	private InterfaceServidor referenciaServidor;
	
	private ITeamEnv teamA;
	private ITeamEnv teamB;
	
	private String nameTeamA;
	private String nameTeamB;
	
	@Override
	public void init(String[] args) {
		
		cliente = new Cliente(1313, "localhost");
		cliente.iniciaCliente();
		referenciaServidor = cliente.getReferenciaServidor();
		
		ConstantesExecucao.start();
		nameTeamA = ConstantesExecucao.nomeTeamA.toLowerCase();
		nameTeamB = ConstantesExecucao.nomeTeamB.toLowerCase();
		
		
		/***Carega as classes de enviroment de cada time***/
		try {
			File customElementsDir = new File(ConstantesExecucao.caminhoEnvironment);
			URLClassLoader uc = new URLClassLoader(new URL[] { customElementsDir.toURI().toURL()});
			teamA = (ITeamEnv)uc.loadClass("jason.ambiente."+ nameTeamA.toUpperCase() + "TeamEnv").newInstance();
			teamB = (ITeamEnv)uc.loadClass("jason.ambiente."+ nameTeamB.toUpperCase() + "TeamEnv").newInstance();
			teamA.setEnvironment(this);
			teamB.setEnvironment(this);
			teamA.setTeamsName(ConstantesExecucao.nomeTeamA, ConstantesExecucao.nomeTeamB);
			teamB.setTeamsName(ConstantesExecucao.nomeTeamB, ConstantesExecucao.nomeTeamA);
			teamA.setup();
			teamB.setup();
			
			referenciaServidor.setStart(true);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean executeAction(String ag, Structure action) {
		
		String splitAg[] = ag.split("_");
		boolean result = false;
		if ( splitAg[INDEX_TEAM].equals(nameTeamA)){
			result = teamA.executeAction(ag, action);
		}
		else if ( splitAg[INDEX_TEAM].equals(nameTeamB)){
			result = teamB.executeAction(ag, action);
		}
		else{
			System.out.println("Nenhum time com esse nome (" +ag+"). Verifique se o nome do agente segue o modelo \"NomeDaEquipe\"_\"nomeDoAgente\".asl");
		}
		
		return result;
	}
	public InterfaceServidor getReInterfaceServidor(){
		return referenciaServidor;
	}
	
	
}